package com.video.chat.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.video.chat.domain.dto.QueryPageDto;
import com.video.chat.domain.entity.MessageEntity;
import com.video.chat.domain.entity.SystemMsgEntity;
import com.video.chat.domain.pojo.MsgLook;
import com.video.chat.domain.pojo.SetMsgStatus;
import com.video.chat.mapper.ChatMessageMapper;
import com.video.chat.mapper.SystemMsgMapper;
import com.video.chat.service.MessageService;
import com.video.chat.service.SystemMsgService;
import com.video.chat.websocket.WebSocketServer;
import com.video.common.result.PageResult;
import com.video.common.result.ResponseResult;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/chat")
@Api(tags = "聊天管理")
@RequiredArgsConstructor
public class chatController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MessageService messageService;
    @Autowired
    private ChatMessageMapper chatMessageMapper;
    @Autowired
    private WebSocketServer webSocketServer;
    @Autowired
    private SystemMsgService systemMsgService;
    @Autowired
    private SystemMsgMapper systemMsgMapper;


    /**
     * 获取聊天记录
     * @return
     */
    @GetMapping("/getMessageList")
    public ResponseResult getMessageList(@RequestParam Long userId,@RequestParam Long friendId){
       List<MessageEntity> list=  messageService.getChatHistory(userId, friendId);
        return ResponseResult.okResult(list);
    }

    /**
     * 获取在线的用户数据
     */
    @GetMapping("/getOnlineUser")
    public ResponseResult getOnlineUser(){
        Set online_users = redisTemplate.opsForSet().members("online_users");
        return ResponseResult.okResult(online_users);
    }
    /**
     * 设置消息读取完毕
     */
    @PostMapping("/updateRead")
    public ResponseResult updateRead(@RequestBody MsgLook msgLook){
        System.out.println("userId"+msgLook.getUserId());
        System.out.println("friendId" + msgLook.getFriendId());
        chatMessageMapper.updateRead(msgLook);
        return ResponseResult.okResult("成功");
    }


    /**
     * 查看系统消息
     */
    @GetMapping("/getSystemMsg")
    public ResponseResult getSystemMsg(QueryPageDto dto){
        dto.validateParams();
        Page<SystemMsgEntity> page = systemMsgService.lambdaQuery()
                .eq(dto.getType() != null, SystemMsgEntity::getType, dto.getType())
                 .like(dto.getKeywords() != null, SystemMsgEntity::getTitle, dto.getKeywords())
                 .or()
                 .like(dto.getKeywords() != null, SystemMsgEntity::getContent, dto.getKeywords())
                .orderByDesc(SystemMsgEntity::getCreatedTime)
                .page(new Page<>(dto.getPage(), dto.getPageSize()));
        PageResult result = new PageResult(page.getTotal(), page.getRecords());
        return ResponseResult.okResult(result);
    }

    /**
     * 新增系统消息
     */
    @PostMapping("/addSystemMsg")
    public ResponseResult addSystemMsg(@RequestBody SystemMsgEntity systemMsgEntity){
        SystemMsgEntity build = systemMsgEntity.builder()
                .isRead("1")
                .revocation(1)
                .createdTime(System.currentTimeMillis())
                .title(systemMsgEntity.getTitle())
                .content(systemMsgEntity.getContent())
                .type(systemMsgEntity.getType())
                .updateTime(System.currentTimeMillis())
                .build();
        systemMsgService.save(build);
        webSocketServer.sendSystemMessageToAllClient(build);
        return ResponseResult.okResult("成功");
    }

    /**
     * 设置系统消息读取完毕
     */
    @PostMapping("/upSysMsg")
    public ResponseResult upSysMsg(@RequestBody SetMsgStatus dto){
        systemMsgMapper.updateRead(dto);
        return ResponseResult.okResult("成功");
    }

    /**
     * 撤回功能
     */
    @PostMapping("/revoke")
    public ResponseResult revoke(@RequestBody SystemMsgEntity dto){
        systemMsgMapper.revokeChange(dto.getId());
        return ResponseResult.okResult("成功");
    }

}
